package com.digital.smartgrow.service.upscale.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digital.smartgrow.common.util.BeanCopyUtil;
import com.digital.smartgrow.dto.upscale.UpScalePageDTO;
import com.digital.smartgrow.dto.upscale.UpscaleDTO;
import com.digital.smartgrow.entity.process.UpProcessItem;
import com.digital.smartgrow.entity.upscale.ScaleItem;
import com.digital.smartgrow.entity.upscale.UpScale;
import com.digital.smartgrow.dao.upscale.UpScaleMapper;
import com.digital.smartgrow.service.upscale.ScaleItemService;
import com.digital.smartgrow.service.upscale.UpScaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 晋升指标 服务实现类
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-12
 */
@Service
public class UpScaleServiceImpl extends ServiceImpl<UpScaleMapper, UpScale> implements UpScaleService {
@Autowired
private ScaleItemService itemService;

    @Override
    public Object getListByPage(UpScalePageDTO pageDTO) {
        Page<UpScale> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<UpScale> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getScaleName()), "scaleName", pageDTO.getScaleName());
        queryWrapper.eq(pageDTO.getScaleState()!=null, "scaleState", pageDTO.getScaleState());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getScaleSeries()), "scaleSeries", pageDTO.getScaleSeries());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getScaleGrade()), "scaleGrade", pageDTO.getScaleGrade());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getScaleYear()), "scaleYear", pageDTO.getScaleYear());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getItemId()), "itemId", pageDTO.getItemId());
        return this.page(page, queryWrapper);
    }

//    @Override
//    public Object getInfo(Integer id) {
//        return this.getById(id);
//    }
@Override
public Object getInfo(Integer id) {
    UpScale upScale = this.getById(id);
    UpscaleDTO upscaleDTO=BeanCopyUtil.copyProperties(upScale,UpscaleDTO::new);
    List<ScaleItem> list = itemService.lambdaQuery().eq(ScaleItem::getSuperId, id).list();
    upscaleDTO.setScaleItemList(list);
    return upscaleDTO;
}

    @Override
    public Object saveBat(UpscaleDTO upScale) {

        if (upScale.getId() != null){
            //更新操作先删除
            this.removeById(upScale.getId());
            //删除原数据
            QueryWrapper<ScaleItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ScaleItem::getSuperId, upScale.getId() );
            itemService.remove(queryWrapper);

        }
        //新增
        UpScale upScale1=new UpScale();
        BeanCopyUtil.copyNotEmptyProperties(upScale, upScale1);
        //BeanUtils.copyProperties(upScale,upScale1);


//        private String scaleName;
//
//        @ApiModelProperty(value = "状态")
//        @TableField("scaleState")
//        private Integer scaleState;
//
//        @ApiModelProperty(value = "系列")
//        @TableField("scaleSeries")
//        private String scaleSeries;
//
//        @ApiModelProperty(value = "级别")
//        @TableField("scaleGrade")
//        private String scaleGrade;
//
//        @ApiModelProperty(value = "年度")
//        @TableField("scaleYear")
//        private String scaleYear;
//
//        @ApiModelProperty(value = "图片")
//        @TableField("imageUrl")
//        private String imageUrl;
        System.out.println("upScale = " + upScale);
        System.out.println("upScale1 = " + upScale1);
        if(this.save(upScale1)){
            String item_id="";
            List<ScaleItem> scaleItemList = BeanCopyUtil.copyListProperties(upScale.getScaleItemList(), ScaleItem::new);
            if(scaleItemList.size()>0){
                for(int i=0;i<scaleItemList.size();i++){
                    scaleItemList.get(i).setSuperId(upScale1.getId()+"");
                    item_id="["+scaleItemList.get(i).getId()+"]";
                }
                upScale1.setItemId(item_id);
                this.updateById(upScale1);
                itemService.saveBatch(scaleItemList);
            }

        }
        return upScale1;
    }
}
